/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.fasepa.sgpf.dao;

import br.gov.fasepa.sgpf.entity.Cargo;
import br.gov.fasepa.sgpf.entity.Servidor;
import br.gov.fasepa.sgpf.entity.TipoCargo;
import br.gov.fasepa.sgpf.factoryDAO.GenericDAO;
import br.gov.fasepa.sgpf.rn.ServidorRN;
import java.util.List;
import javax.persistence.Query;

/**
 *
 * @author marcosdj
 */
public class ServidorDAO extends GenericDAO<Servidor> {

    public Servidor obter(String matricula) {
        String query = "Servidor.findByMatricula";
        final Query q = getEntityManager().createNamedQuery(query).
                setParameter("matricula", matricula);
        try {
            return (Servidor) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public Servidor obterPorEmail(String email) {
        String query = "Servidor.findByEmail";
        final Query q = getEntityManager().createNamedQuery(query).
                setParameter("email", email);
        try {
            return (Servidor) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public List<Servidor> ListarDecrescente() {
        String query = "SELECT s FROM Servidor s ORDER BY s.id desc";
        final Query q = getEntityManager().createQuery(query);
        try {
            return (List<Servidor>) q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    public List<Servidor> ListarServidorFiltro(Cargo cargo, TipoCargo tipoDeCargo) {        
        String query = "SELECT s FROM Servidor s WHERE s.cargo = :cargo "
                + "AND s.tipoDeCargo = :tipoDeCargo ORDER BY s.id desc";        
        final Query q = getEntityManager().createQuery(query).
                setParameter("cargo",cargo).
                setParameter("tipoDeCargo",tipoDeCargo);
        try {
            return (List<Servidor>) q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }
    

}
